package com.store.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.store.api.ApiExpress;
import com.store.model.*;
import com.store.model.config.ApplicationEntity;
import com.store.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;

@Service
public class ExpressService {

    private org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(ApiExpress.class);

    @Autowired
    private ExpressinfoService expressinfoService;

    @Autowired
    private OrderinfoService orderinfoService;

    @Autowired
    private OrderdetailService orderdetailService;

    @Autowired
    private UseraddressService useraddressService;

    @Autowired
    private ApplicationEntity applicationEntity;


    public static final int WEIGHT = 100;
    //pro
    public static int devId = 1536875034;
    //pro
    public static String shopId = "BR001";
    //pro
    public static String APP_SECRET = "739eda4ffd917b2c6273ba13d2cf470d";

//    //test
//    public static int devId = 1526425029;
//    //test
//    public static String shopId = "SH001";
//    //test
//    public static String APP_SECRET = "2ad359046b072a95de7b85e2edf63d1b";


    public static final String ORDER_SOURCE = "贝朗";

    public static final String CREATE_ORDER = "https://commit-openic.sf-express.com/open/api/external/createorder?sign=%1$s";

    public static final String RIDERH_5VIEW = "https://commit-openic.sf-express.com/open/api/external/riderh5view?sign=%1$s";

    public static final String PRECREATE_ORDER = "https://commit-openic.sf-express.com/open/api/external/precreateorder?sign=%1$s";

    public static final String LIST_ORDER_FEED = "https://commit-openic.sf-express.com/open/api/external/listorderfeed?sign=%1$s";

    public static final String RIDER_LATEST_POSITION = "https://commit-openic.sf-express.com/open/api/external/riderlatestposition?sign=%1$s";

    public static final String CANCEL_ORDER = "https://commit-openic.sf-express.com/open/api/external/cancelorder?sign=%1$s";

    public static int getSecord() {
        return (int) (System.currentTimeMillis() / 1000);
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        ExpressService e = new ExpressService();
        e.riderlatestposition("3267158756242242563");
        System.out.println("\u8ba2\u5355\u4e0d\u5b58\u5728\u3010\u8ba2\u5355\u4e0d\u5b58\u5728\u6216\u8005\u5df2\u5931\u6548\uff0c\u6700\u591a\u652f\u6301\u67e5\u8be2\u8fd1120\u5929\u7684\u8ba2\u5355\u3011");
    }

    public Map<String, Object> currentEnv() {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("shopId", shopId);
        return returnMap;
    }

    public void switchEnv(String env) {
        Map<String, Object> sfMap = applicationEntity.getSf();
        Map<String, Object> devMap = (Map<String, Object>) sfMap.get("dev");
        Map<String, Object> proMap = (Map<String, Object>) sfMap.get("pro");
        if ("dev".equals(env)) {
            devId = Integer.parseInt(devMap.get("dev_id").toString());
            shopId = String.valueOf(devMap.get("shop_id"));
            APP_SECRET = String.valueOf(devMap.get("dev_key"));
        } else {
            devId = Integer.parseInt(proMap.get("dev_id").toString());
            shopId = String.valueOf(proMap.get("shop_id"));
            APP_SECRET = String.valueOf(proMap.get("dev_key"));
        }
    }

    public String cancelorder(String orderno) {
        Map<String, Object> preCreateOrderMap = new HashMap<>();
        String responseStr = "";
        try {
            ExpressinfoModel expressinfoModel = new ExpressinfoModel();
            expressinfoModel.setOrderno(orderno);
            expressinfoModel.setApitype("createorder");
            expressinfoModel.setErrorcode(0);
            expressinfoModel = expressinfoService.getModelById(expressinfoModel);
            if (null == expressinfoModel) {
                return "没有找到订单";
            }
            preCreateOrderMap.put("dev_id", devId);
            preCreateOrderMap.put("shop_id", shopId);
            preCreateOrderMap.put("order_id", expressinfoModel.getSforderid());
            preCreateOrderMap.put("order_type", 1);
            preCreateOrderMap.put("shop_type", 2);
            preCreateOrderMap.put("cancel_reason", "用户取消下单");
            preCreateOrderMap.put("push_time", getSecord());
            String content = JSON.toJSONString(preCreateOrderMap, SerializerFeature.MapSortField);
            SFApiUtil apiUtil = new SFApiUtil();
            String signStr = apiUtil.sign(content);
            String requestUrl = CANCEL_ORDER;
            requestUrl = String.format(requestUrl, signStr);
            responseStr = HttpRequest.sendPost(requestUrl, content, null);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap = JSON.parseObject(responseStr);
            int errorCode = (int) returnMap.get("error_code");
            if (3006 == errorCode) {
                return "商品超出配送距离";
            } else if (0 != errorCode) {
                ExpressinfoModel infoModel = new ExpressinfoModel();
                infoModel.setErrorcode(errorCode);
                infoModel.setApitype(ConstantVariable.CALCELORDER_ERR);
                infoModel.setContent(responseStr + ConstantVariable.SPLIT + JSON.toJSONString(preCreateOrderMap));
                infoModel.setCreatetime(DateUtil.getTime());
                expressinfoService.insert(infoModel);
                return responseStr;
            }
            return "取消成功";

        } catch (Exception e) {
            e.printStackTrace();
            ExpressinfoModel infoModel = new ExpressinfoModel();
            infoModel.setErrorcode(ConstantVariable.EXCEPTION_CODE);
            infoModel.setApitype(ConstantVariable.PRECREATEORDER_ERR);
            infoModel.setContent(responseStr + ConstantVariable.SPLIT + JSON.toJSONString(preCreateOrderMap) + ConstantVariable.SPLIT + e.getMessage());
            infoModel.setCreatetime(DateUtil.getTime());
            expressinfoService.insert(infoModel);
        }
        return "取消成功";
    }

    public String riderlatestposition(String sfOrderId) {
        String responseStr = "";
        try {
            Map<String, Object> riderlatestpositionMap = new HashMap<>();
            riderlatestpositionMap.put("dev_id", devId);
            riderlatestpositionMap.put("order_id", sfOrderId);
            riderlatestpositionMap.put("order_type", 2);
            riderlatestpositionMap.put("shop_id", shopId);
            riderlatestpositionMap.put("shop_type", 2);
            riderlatestpositionMap.put("push_time", getSecord());

            String content = JSON.toJSONString(riderlatestpositionMap, SerializerFeature.MapSortField);
            SFApiUtil apiUtil = new SFApiUtil();
            String signStr = apiUtil.sign(content);
            String requestUrl = RIDER_LATEST_POSITION;
            requestUrl = String.format(requestUrl, signStr);
            responseStr = HttpRequest.sendPost(requestUrl, content, null);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap = JSON.parseObject(responseStr);
            int errorCode = (int) returnMap.get("error_code");
            if (errorCode == 0) {
                ExpressinfoModel expressinfoModel = expressinfoService.getObjBySfid(sfOrderId);
                if (null != expressinfoModel) {
                    ExpresspositionModel expresspositionModel = new ExpresspositionModel();
                    expresspositionModel.setOrderno(expressinfoModel.getOrderno());
                    expresspositionModel.setSforderid(expressinfoModel.getSforderid());
                    expresspositionModel.setRidername(String.valueOf(returnMap.get("rider_name")));
                    expresspositionModel.setRiderphone(String.valueOf(returnMap.get("rider_phone")));
                    expresspositionModel.setRiderlng(String.valueOf(returnMap.get("rider_lng")));
                    expresspositionModel.setRiderlat(String.valueOf(returnMap.get("rider_at")));
                    expresspositionModel.setUploadtime(String.valueOf(returnMap.get("upload_time")));
                }
            } else {
                ExpressinfoModel infoModel = new ExpressinfoModel();
                infoModel.setErrorcode(errorCode);
                infoModel.setApitype("riderlatestposition");
                infoModel.setContent(responseStr);
                infoModel.setCreatetime(DateUtil.getTime());
                expressinfoService.insert(infoModel);
            }
            return responseStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String preCreateOrderTest(String user_lng, String user_lat, String user_address, String city_name, int weight) {
        Map<String, Object> preCreateOrderMap = new HashMap<>();
        String responseStr = "";
        try {
            preCreateOrderMap.put("dev_id", devId);
            preCreateOrderMap.put("shop_id", shopId);
            preCreateOrderMap.put("shop_type", 2);
            preCreateOrderMap.put("user_lng", user_lng);
            preCreateOrderMap.put("user_lat", user_lat);
            preCreateOrderMap.put("user_address", user_address);
            preCreateOrderMap.put("city_name", city_name);
            preCreateOrderMap.put("weight", weight);
            preCreateOrderMap.put("product_type", 99);
            preCreateOrderMap.put("is_appoint", "1");
            preCreateOrderMap.put("expect_time", getSecord());
            preCreateOrderMap.put("pay_type", 1);
            preCreateOrderMap.put("is_insured", 0);
            preCreateOrderMap.put("push_time", getSecord());

            String content = JSON.toJSONString(preCreateOrderMap, SerializerFeature.MapSortField);
            SFApiUtil apiUtil = new SFApiUtil();
            String signStr = apiUtil.sign(content);
            String requestUrl = PRECREATE_ORDER;
            requestUrl = String.format(requestUrl, signStr);
            responseStr = HttpRequest.sendPost(requestUrl, content, null);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap = JSON.parseObject(responseStr);
            int errorCode = (int) returnMap.get("error_code");
            if (3006 == errorCode) {
                return "商品超出配送距离";
            } else if (0 != errorCode) {
                ExpressinfoModel infoModel = new ExpressinfoModel();
                infoModel.setErrorcode(errorCode);
                infoModel.setApitype(ConstantVariable.PRECREATEORDER_ERR);
                infoModel.setContent(content + responseStr + ConstantVariable.SPLIT + JSON.toJSONString(preCreateOrderMap));
                infoModel.setCreatetime(DateUtil.getTime());
                expressinfoService.insert(infoModel);
                return "下单失败，请稍后再试";
            }
            return responseStr;

        } catch (Exception e) {
            e.printStackTrace();
            ExpressinfoModel infoModel = new ExpressinfoModel();
            infoModel.setErrorcode(ConstantVariable.EXCEPTION_CODE);
            infoModel.setApitype(ConstantVariable.PRECREATEORDER_ERR);
            infoModel.setContent(responseStr + ConstantVariable.SPLIT + JSON.toJSONString(preCreateOrderMap) + ConstantVariable.SPLIT + e.getMessage());
            infoModel.setCreatetime(DateUtil.getTime());
            expressinfoService.insert(infoModel);
        }
        return responseStr;
    }

    public String preCreateOrder(String user_lng, String user_lat, String user_address, String city_name, int weight) {
        Map<String, Object> preCreateOrderMap = new HashMap<>();
        String responseStr = "";
        try {
            preCreateOrderMap.put("dev_id", devId);
            preCreateOrderMap.put("shop_id", shopId);
            preCreateOrderMap.put("shop_type", 2);
            preCreateOrderMap.put("user_lng", user_lng);
            preCreateOrderMap.put("user_lat", user_lat);
            preCreateOrderMap.put("user_address", user_address);
            preCreateOrderMap.put("city_name", city_name);
            preCreateOrderMap.put("weight", weight);
            preCreateOrderMap.put("product_type", 99);
            preCreateOrderMap.put("is_appoint", "1");
            preCreateOrderMap.put("expect_time", getSecord());
            preCreateOrderMap.put("pay_type", 1);
            preCreateOrderMap.put("is_insured", 0);
            preCreateOrderMap.put("push_time", getSecord());
            String content = JSON.toJSONString(preCreateOrderMap, SerializerFeature.MapSortField);
            SFApiUtil apiUtil = new SFApiUtil();
            String signStr = apiUtil.sign(content);
            String requestUrl = PRECREATE_ORDER;
            requestUrl = String.format(requestUrl, signStr);
            responseStr = HttpRequest.sendPost(requestUrl, content, null);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap = JSON.parseObject(responseStr);
            int errorCode = (int) returnMap.get("error_code");
            if (3006 == errorCode) {
                return "商品超出配送距离";
            } else if (0 != errorCode) {
                ExpressinfoModel infoModel = new ExpressinfoModel();
                infoModel.setErrorcode(errorCode);
                infoModel.setApitype(ConstantVariable.PRECREATEORDER_ERR);
                infoModel.setContent(content + responseStr + ConstantVariable.SPLIT + JSON.toJSONString(preCreateOrderMap));
                infoModel.setCreatetime(DateUtil.getTime());
                expressinfoService.insert(infoModel);
                return "下单失败，请稍后再试";
            }
            return "";

        } catch (Exception e) {
            e.printStackTrace();
            ExpressinfoModel infoModel = new ExpressinfoModel();
            infoModel.setErrorcode(ConstantVariable.EXCEPTION_CODE);
            infoModel.setApitype(ConstantVariable.PRECREATEORDER_ERR);
            infoModel.setContent(responseStr + ConstantVariable.SPLIT + JSON.toJSONString(preCreateOrderMap) + ConstantVariable.SPLIT + e.getMessage());
            infoModel.setCreatetime(DateUtil.getTime());
            expressinfoService.insert(infoModel);
        }
        return "";
    }

    public String riderh5view(String sfOrderId, String orderNo) throws UnsupportedEncodingException {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("dev_id", devId);
        dataMap.put("order_id", sfOrderId);
        dataMap.put("order_type", 1); //1、顺丰订单号 2、商家订单号
        dataMap.put("shop_id", shopId);
        dataMap.put("shop_type", 2);
        dataMap.put("push_time", getSecord());

        String content = JSON.toJSONString(dataMap, SerializerFeature.MapSortField);
        SFApiUtil apiUtil = new SFApiUtil();
        String signStr = apiUtil.sign(content);
        String requestUrl = RIDERH_5VIEW;
        requestUrl = String.format(requestUrl, signStr);
        String responseStr = HttpRequest.sendPost(requestUrl, content, null);
        Map<String, Object> returnMap = JSON.parseObject(responseStr);
        int errorCode = Integer.parseInt(String.valueOf(returnMap.get("error_code")));
        String url = "";
        if (errorCode == 0) {
            Map<String, Object> resultMap = JSON.parseObject(String.valueOf(returnMap.get("result")));
            url = String.valueOf(resultMap.get("url"));
            ExpressinfoModel expressinfoModel = new ExpressinfoModel();
            expressinfoModel.setSforderid(sfOrderId);
            expressinfoModel.setOrderno(orderNo);
            expressinfoModel.setContent(url);
            expressinfoModel.setApitype("riderh5view");
            expressinfoModel.setErrorcode(0);
            expressinfoService.insert(expressinfoModel);
        }
        return url;
    }

    public List<Map<String, Object>> listorderfeed(String sfOrderId, String orderNo) throws UnsupportedEncodingException {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("dev_id", devId);
        dataMap.put("order_id", sfOrderId);
        dataMap.put("shop_type", 2);
        dataMap.put("push_time", getSecord());

        String content = JSON.toJSONString(dataMap, SerializerFeature.MapSortField);
        SFApiUtil apiUtil = new SFApiUtil();
        String signStr = apiUtil.sign(content);
        String requestUrl = LIST_ORDER_FEED;
        requestUrl = String.format(requestUrl, signStr);
        String responseStr = HttpRequest.sendPost(requestUrl, content, null);
        Map<String, Object> returnMap = JSON.parseObject(responseStr);
        int errorCode = Integer.parseInt(String.valueOf(returnMap.get("error_code")));
        List<Map<String, Object>> dataList = new ArrayList<>();
        if (errorCode == 0) {
            Map<String, Object> resultMap = (Map<String, Object>) returnMap.get("result");
            dataList = (List<Map<String, Object>>) resultMap.get("feed");
            ExpressinfoModel expressinfoModel = new ExpressinfoModel();
            expressinfoModel.setSforderid(sfOrderId);
            expressinfoModel.setOrderno(orderNo);
            expressinfoModel.setApitype("listorderfeed");
            expressinfoModel.setErrorcode(0);
            expressinfoModel.noPage();
            List<ExpressinfoModel> expressinfoModels = expressinfoService.getList(expressinfoModel);
            List<String> idList = new ArrayList<>();
            for (ExpressinfoModel e : expressinfoModels) {
                idList.add(e.getEsid());
            }

            if (idList.size() != 0) {
                expressinfoService.batchDelete(idList);
            }
            expressinfoModel.setContent(JSON.toJSONString(dataList));
            expressinfoService.insert(expressinfoModel);
        }
        return dataList;
    }

    @Autowired
    private RegularorderService regularorderService;

    @Autowired
    private RegularsuborderService regularsuborderService;

    public void createRegularSubOrder(String subOrderNo) {

        try {
            RegularsuborderModel regularsuborderModel = new RegularsuborderModel();
            regularsuborderModel.setSuborderno(subOrderNo);
            regularsuborderModel = regularsuborderService.getModelById(regularsuborderModel);
            RegularorderModel regularorderModel = new RegularorderModel();
            regularorderModel.setOrderno(regularorderModel.getOrderno());
            regularorderModel = regularorderService.getModelById(regularorderModel);

            Map<String, Object> receiveMap = new HashMap<>();
            receiveMap.put("user_name", regularsuborderModel.getReceivename());
            receiveMap.put("user_phone", regularsuborderModel.getReceivephone());
            receiveMap.put("user_address", regularsuborderModel.getReceiveaddress());
            receiveMap.put("user_lat", regularsuborderModel.getReceivelat()); //维度
            receiveMap.put("user_lng", regularsuborderModel.getReceivelng()); //经度
//            receiveMap.put("city_name", regularsuborderModel.getReceivecity());

            Map<String, Object> orderDetailMap = new HashMap<>();
            List<Map<String, Object>> productDetailList = new ArrayList();

            Map<String, Object> productDetail = new HashMap<>();
            productDetail.put("product_name", regularsuborderModel.getProductname());
            productDetail.put("product_num", 1);
            productDetailList.add(productDetail);


            orderDetailMap.put("product_detail", productDetailList);
            orderDetailMap.put("product_num", 1);
            orderDetailMap.put("product_type_num", 1);
            orderDetailMap.put("weight_gram", regularsuborderModel.getUnit());
            orderDetailMap.put("product_type", 99);
            String totalPrice = MathUtil.multiply(regularorderModel.getPrice(), 100);

            orderDetailMap.put("total_price", Integer.parseInt(subZeroAndDot(totalPrice)));

            Map<String, Object> createOrderMap = new HashMap<>();
            createOrderMap.put("dev_id", devId);
            createOrderMap.put("shop_id", shopId);
            createOrderMap.put("shop_type", "2");
            createOrderMap.put("shop_order_id", regularsuborderModel.getSuborderno());
            createOrderMap.put("order_source", ORDER_SOURCE);
            createOrderMap.put("pay_type", 1);
            createOrderMap.put("order_time", getSecord()); //用户下单时间
            createOrderMap.put("is_appoint", 1); //是否预约单

            createOrderMap.put("expect_time", fmtSubOrderSendTime(regularsuborderModel.getSendtime(), regularsuborderModel.getEndstagetime())); //预约单需必传,秒级时间戳

            createOrderMap.put("is_insured", 0); //是否保价
            createOrderMap.put("push_time", getSecord());
            createOrderMap.put("version", 17);
            createOrderMap.put("receive", receiveMap);
            createOrderMap.put("order_detail", orderDetailMap);

            String content = JSON.toJSONString(createOrderMap, SerializerFeature.MapSortField);
            SFApiUtil apiUtil = new SFApiUtil();
            String signStr = apiUtil.sign(content);
            String requestUrl = CREATE_ORDER;
            requestUrl = String.format(requestUrl, signStr);
            String resultStr = HttpRequest.sendPost(requestUrl, content, null);
            Map<String, Object> returnMap = JSON.parseObject(resultStr);
            Map<String, Object> resultMap = (Map<String, Object>) returnMap.get("result");
            ExpressinfoModel model = new ExpressinfoModel();
            Integer errorCode = (int) (returnMap.get(ConstantVariable.SF_ERROR_CODE));
            if (errorCode != 0) {
                //
            } else {
                model.setErrorcode(errorCode);
                model.setOrderno(subOrderNo);
                model.setContent(resultStr);
                model.setApitype("createorder");
                model.setSforderid(String.valueOf(resultMap.get("sf_order_id")));
                expressinfoService.insert(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void createOrder(String orderId) {
        OrderinfoModel orderinfoModel = new OrderinfoModel();
        orderinfoModel.setOrderid(orderId);
        orderinfoModel = orderinfoService.getModelById(orderinfoModel);
        if (null == orderinfoModel) {
            orderinfoModel = new OrderinfoModel();
            orderinfoModel.setOrderno(orderId);
            orderinfoModel = orderinfoService.getModelById(orderinfoModel);
        }
        if (!"3".equals(orderinfoModel.getSendtype())) {
            return;
        }

        OrderdetailModel orderdetailModel = new OrderdetailModel();
        List<OrderdetailModel> orderDetailList = orderdetailService.getList(orderdetailModel);

        String orderNo = orderinfoModel.getOrderno();
        ExpressinfoModel expressinfoModel = new ExpressinfoModel();
        expressinfoModel.setOrderno(orderNo);
        expressinfoModel.setErrorcode(0);
        expressinfoModel.setApitype("createorder");
        expressinfoModel = expressinfoService.getModelById(expressinfoModel);
        if (null != expressinfoModel) {
            return;
        }

        try {
            Map<String, Object> receiveMap = new HashMap<>();
            receiveMap.put("user_name", orderinfoModel.getReceiveuser());
            receiveMap.put("user_phone", orderinfoModel.getReceivephone());
            receiveMap.put("user_address", orderinfoModel.getReceiveaddress());
            receiveMap.put("user_lat", orderinfoModel.getLat()); //维度
            receiveMap.put("user_lng", orderinfoModel.getLng()); //经度
//            receiveMap.put("city_name", useraddressModel.getCityname());

            Map<String, Object> orderDetailMap = new HashMap<>();
            List<Map<String, Object>> productDetailList = new ArrayList();
            for (int i = 0; i < orderDetailList.size(); i++) {
                OrderdetailModel detailModel = new OrderdetailModel();
                Map<String, Object> productDetail = new HashMap<>();
                productDetail.put("product_name", detailModel.getVegetablename());
                productDetail.put("product_num", 1);
                productDetailList.add(productDetail);
            }
            orderDetailMap.put("product_detail", productDetailList);
            orderDetailMap.put("product_num", orderDetailList.size());
            orderDetailMap.put("product_type_num", orderDetailList.size());
            orderDetailMap.put("weight_gram", orderDetailList.size() * 50);
            orderDetailMap.put("product_type", 99);

            String price = MathUtil.multiply(orderinfoModel.getSaleprice(), 100);
            orderDetailMap.put("total_price", Integer.parseInt(subZeroAndDot(price)));

            Map<String, Object> createOrderMap = new HashMap<>();
            createOrderMap.put("dev_id", devId);
            createOrderMap.put("shop_id", shopId);
            createOrderMap.put("shop_type", "2");
            createOrderMap.put("shop_order_id", orderId);
            createOrderMap.put("order_source", ORDER_SOURCE);
            createOrderMap.put("pay_type", 1);
            createOrderMap.put("order_time", getSecord()); //用户下单时间
            createOrderMap.put("is_appoint", 1); //是否预约单
            createOrderMap.put("expect_time", fmtSendTime(orderinfoModel.getSendtime())); //预约单需必传,秒级时间戳

            createOrderMap.put("is_insured", 0); //是否保价
            createOrderMap.put("push_time", getSecord());
            createOrderMap.put("version", 17);
            createOrderMap.put("receive", receiveMap);
            createOrderMap.put("order_detail", orderDetailMap);

            String content = JSON.toJSONString(createOrderMap, SerializerFeature.MapSortField);
            SFApiUtil apiUtil = new SFApiUtil();
            String signStr = apiUtil.sign(content);
            String requestUrl = CREATE_ORDER;
            requestUrl = String.format(requestUrl, signStr);
            String resultStr = HttpRequest.sendPost(requestUrl, content, null);
            Map<String, Object> returnMap = JSON.parseObject(resultStr);
            Map<String, Object> resultMap = (Map<String, Object>) returnMap.get("result");
            ExpressinfoModel model = new ExpressinfoModel();
            Integer errorCode = (int) (returnMap.get(ConstantVariable.SF_ERROR_CODE));
            if (errorCode != 0) {
                model.setErrorcode(errorCode);
                model.setOrderno(orderinfoModel.getOrderno());
                model.setContent(resultStr);
                model.setApitype("createorder");
                model.setSforderid("");
                model.setOrderno(content + ConstantVariable.SPLIT + resultStr);
                expressinfoService.insert(model);
                //
            } else {
                model.setErrorcode(errorCode);
                model.setOrderno(orderinfoModel.getOrderno());
                model.setContent(resultStr);
                model.setApitype("createorder");
                model.setSforderid(String.valueOf(resultMap.get("sf_order_id")));
                model.setOrderno(orderinfoModel.getOrderno());
                expressinfoService.insert(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static int fmtSendTime(String sendTime) {
        String[] replaceStrArr = new String[]{"周一", "周一 ", "周二", "周二 ", "周三", "周三 ", "周四", "周四 ", "周五", "周五 ", "周六", "周六 ", "周日", "周日 "};
        try {
            if (sendTime.contains("立即送出")) {
                return getSecord();
            } else {
                for (String replaceStr : replaceStrArr) {
                    sendTime = sendTime.replaceAll(replaceStr, "");
                    if (!sendTime.contains("周")) {
                        break;
                    }
                }

                Calendar cal = Calendar.getInstance();

                String[] dateArr = sendTime.split(" ");
                String[] ymd = dateArr[0].split("-");
                String[] hourDate = dateArr[1].split("-");
                String hour = String.valueOf(cal.getTime().getHours());
                if (hourDate.length > 1) {
                    hour = hourDate[1].replaceAll(":00", "");
                }
                cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]), Integer.parseInt(hour), 0);
                return (int) (cal.getTime().getTime() / 1000);
            }
        } catch (Exception e) {
            return getSecord();
        }
    }

    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    private int fmtSubOrderSendTime(String day, String time) {
        try {
            String[] ymd = day.split("-");
            String hour = time.replaceAll(":00", "");
            Calendar cal = Calendar.getInstance();
            cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]), Integer.parseInt(hour), 0);
            return (int) (cal.getTime().getTime() / 1000);
        } catch (Exception e) {
            return getSecord();
        }
    }

}